package com.fourth.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.fourth.dao.UserDao;
import com.fourth.service.UserService;

import com.ljx.entity.UserInfo;


import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @author LJX
 * @Description: TODO
 * @Date: 2023/3/3 21:19
 * @Version 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserInfo> implements UserService {

	@Autowired(required = false)
	private UserDao userDao;

	@Override
	public Result updatePassword(String telephone, String newPassword) {
		// 密码加密器
		BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
		// 根据工具类查询出token，token有我们保存的用户信息根据信息查询用户电话，
		// 看看电话号码是不是相同，相同就修改密码，不相同就不修改，
		String token = WebUtils.getRequest().getHeader("token");
		Map<String, Object> map = JwtUtil.getTokenChaim(token);
		Object username = map.get("username");
		if(username.equals(telephone)){
			String newPassword1 = passwordEncoder.encode(newPassword);
			userDao.updatePassword(telephone, newPassword1);
			return new Result(2000,"密码修改成功",null);
			}

		return new Result(5000,"密码修改失败",null);
		}
}
